from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

from addconnection_ui import *
import uiplatform

class AddConnection(QDialog, Ui_AddConnection):
    
    """Add/modify connection"""
    
    def __init__(self, *args):
        QDialog.__init__(self, *args)
        self.setupUi(self)
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
        self.setFocusProxy(self.editName)
        for w in [self.editName, self.editGroup, self.editUser, 
                  self.editGateway]:
            self.connect(w, SIGNAL("textChanged(const QString &)"), 
                         self.onTextChange)
        if uiplatform.name == "maemo":
            self.editGroupPassword.setEchoMode(QLineEdit.PasswordEchoOnEdit)
            self.editUserPassword.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        self.editNattMode.addItems(
            ["NAT-T (default)", "None", "Force NAT-T", "Cisco UDP"])
        self.editVendor.addItems(["Cisco (default)", "Netscreen"])
        self.editDh.addItems(["DH1", "DH2 (default)", "DH5"])
        self.editPfs.addItems(["None", "DH1", "DH2", "DH5", "Server (default)"])
        self.editIfMode.addItems(["TUN (default)", "TAP"])
        self.editAuthMode.addItems(["Pre-Shared Key (default)", 
                                    "Server + Client Certificate",
                                    "Server Certificate + XAuth"])
        self.connect(self.buttonBox.button(QDialogButtonBox.Reset), 
            SIGNAL("clicked()"), self.resetFields)

        # FIXME: Work around (Py)Qt bug of disconnected Ok and Canel buttons
        self.connect(self.buttonBox.button(QDialogButtonBox.Ok), 
            SIGNAL("clicked()"), self.accept)
        self.connect(self.buttonBox.button(QDialogButtonBox.Cancel), 
            SIGNAL("clicked()"), self.reject)

    def setFields(self, fields):
        """Set input fields from connection"""
        self.editName.setText(fields["name"])
        self.editGateway.setText(fields["gateway"])
        self.editGroup.setText(fields["group"])
        self.editGroupPassword.setText(fields["groupPassword"])
        self.editUser.setText(fields["user"])
        self.editUserPassword.setText(fields["userPassword"])
        self.editDomain.setText(fields["domain"])
        self.editNattMode.setCurrentIndex(fields["nattMode"])
        self.editVendor.setCurrentIndex(fields["vendor"])
        self.editDh.setCurrentIndex(fields["dh"])
        self.editPfs.setCurrentIndex(fields["pfs"])
        self.editEsd.setCheckState(fields["esd"])
        self.editEne.setCheckState(fields["ene"])
        self.editIfMode.setCurrentIndex(fields["ifMode"])
        self.editLocalAddress.setText(fields["localAddress"])
        self.editLocalPort.setValue(fields["localPort"])
        self.editLocalUdpPort.setValue(fields["localUdpPort"])
        self.editDpdIdle.setValue(fields["dpdIdle"])
        self.editAuthMode.setCurrentIndex(fields["authMode"])
        self.editCaFile.setText(fields["caFile"])
        self.editCaDir.setText(fields["caDir"])
        self.editTargetNetwork.setText(fields["targetNetwork"])
        self.editScript.setText(fields["script"])
        self.onTextChange()

    def resetFields(self):
        """Reset input fields to default values"""
        self.editName.setText("")
        self.editGateway.setText("")
        self.editGroup.setText("")
        self.editGroupPassword.setText("")
        self.editUserPassword.setText("")
        self.editDomain.setText("")
        self.editNattMode.setCurrentIndex(0)
        self.editVendor.setCurrentIndex(0)
        self.editDh.setCurrentIndex(1)
        self.editPfs.setCurrentIndex(4)
        self.editEsd.setCheckState(0)
        self.editEne.setCheckState(0)
        self.editIfMode.setCurrentIndex(0)
        self.editLocalAddress.setText("0.0.0.0")
        self.editLocalPort.setValue(500)
        self.editLocalUdpPort.setValue(10000)
        self.editDpdIdle.setValue(300)
        self.editAuthMode.setCurrentIndex(0)
        self.editCaFile.setText("")
        self.editCaDir.setText("")
        self.editTargetNetwork.setText("0.0.0.0/0.0.0.0")
        self.editScript.setText("")
        self.onTextChange()

    def fields(self):
        """Return input fields' contents as a connection sequence"""
        return {
            "name": self.editName.text(),
            "gateway": self.editGateway.text(),
            "group": self.editGroup.text(),
            "groupPassword": self.editGroupPassword.text(),
            "user": self.editUser.text(),
            "userPassword": self.editUserPassword.text(),
            "domain": self.editDomain.text(),
            "nattMode": self.editNattMode.currentIndex(),
            "vendor": self.editVendor.currentIndex(),
            "dh": self.editDh.currentIndex(),
            "pfs": self.editPfs.currentIndex(),
            "esd": self.editEsd.checkState(),
            "ene": self.editEne.checkState(),
            "ifMode": self.editIfMode.currentIndex(),
            "localAddress": self.editLocalAddress.text(),
            "localPort": self.editLocalPort.value(),
            "localUdpPort": self.editLocalUdpPort.value(),
            "dpdIdle": self.editDpdIdle.value(),
            "authMode": self.editAuthMode.currentIndex(),
            "caFile": self.editCaFile.text(),
            "caDir": self.editCaDir.text(),
            "targetNetwork": self.editTargetNetwork.text(),
            "script": self.editScript.text(),
        }
        
    def onTextChange(self):
        """Enable/disable OK button depending on the content of input fields"""
        enabled = len(self.editName.text()) and len(self.editUser.text()) and \
                  len(self.editGroup.text()) and len(self.editGateway.text())
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enabled)          
